package com.example.javachatroomextend.mapper;

import com.example.javachatroomextend.model.Session;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SessionMapper {
    @Select("select sessionId from session_user where userId=#{userId}")
    List<Integer> getSessionIdByUserId(@Param("userId") Integer userId);

    @Select("select userId from session_user where sessionId=#{sessionId} and userId != #{userId}")
    Integer getFriendUserIdBySessionId(@Param("sessionId") Integer sessionId,@Param("userId") Integer userId);

    @Select("select lastTime,lastMessage from session where sessionId=#{sessionId}")
    Session getSessionBySessionId(@Param("sessionId") Integer sessionId);

    Integer createSession(Session session);

    @Insert("insert into session_user values(#{sessionId},#{userId})")
    Integer addSessionUser(@Param("sessionId") Integer sessionId,@Param("userId") Integer userId);

    @Update("update session set lastTime=#{lastTime},lastMessage=#{lastMessage} where sessionId=#{sessionId}")
    Integer updateLastTimeAndLastMessage(@Param("lastTime") String lastTime,@Param("lastMessage") String lastMessage,@Param("sessionId") Integer sessionId);

    List<Integer> getSessionIdByUserName(@Param("username") String username,@Param("userId") Integer userId);
}
